/*
 * Copyright (C) 2012 Fabien Barbero
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights 
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.flickr.activities;

import java.util.List;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.flickr.R;
import com.flickr.api.ImageURL;
import com.flickr.api.auth.Caller;
import com.flickr.api.auth.OAuthHandler;
import com.flickr.api.photosets.Photoset;
import com.flickr.api.photosets.PhotosetsServiceImpl;
import com.flickr.presenters.PhotosetsPresenter;
import com.flickr.tasks.BitmapLoaderTask;
import com.flickr.views.PhotosetsView;


public class PhotosetsActivity extends Activity implements PhotosetsView, OnItemClickListener
{
    private static final String TAG = PhotosetsActivity.class.getSimpleName();
    
    private PhotosetsServiceImpl photosetsService;
    private PhotosetsPresenter photosetsPresenter;
    private GridView grid;

    private List<Photoset> mSets;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photosets_activity);
        
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        
        grid = (GridView) findViewById(R.id.photosetsGrid);
        grid.setOnItemClickListener(this);
        
        OAuthHandler oauthHandler = OAuthHandler.getHandler();
        
        // Initialization of the Flickr services
        Caller caller = (Caller) getIntent().getExtras().getSerializable("Caller");

        photosetsService = new PhotosetsServiceImpl(oauthHandler);

        photosetsPresenter = new PhotosetsPresenter(this, photosetsService);
        photosetsPresenter.loadPhotosets(caller);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // app icon in action bar clicked; go home
                Intent intent = new Intent(this, DashboardActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        Photoset set = mSets.get(position);
        Log.d(TAG, "Opening photoset " + set.getTitle());
        
        Intent intent = new Intent(this, PhotosetGalleryActivity.class);
        intent.putExtra(PhotosetGalleryActivity.PHOTOSET_KEY, set);
        startActivity(intent);
    }
    
    @Override
    public void setPhotosets(final List<Photoset> sets)
    {
        mSets = sets;
        
        runOnUiThread(new Runnable()
        {
            
            @Override
            public void run()
            {
                grid.setAdapter(new PhotosetsAdapter(sets, PhotosetsActivity.this));
            }
            
        });
        
    }
    
    private static class PhotosetsAdapter extends BaseAdapter {

        private final List<Photoset> mSets;
        private final Context mContext;
        private final LayoutInflater inflater;

        private PhotosetsAdapter(List<Photoset> sets, Context context) {
            mSets = sets;
            mContext = context;
            inflater = LayoutInflater.from(context);
        }
        
        @Override
        public int getCount()
        {
            return mSets.size();
        }

        @Override
        public Photoset getItem(int position)
        {
            return mSets.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            if(convertView == null) {
                convertView = inflater.inflate(R.layout.photoset_item, null);
            }
            
            ImageView imageView = (ImageView) convertView.findViewById(R.id.photosetImage);
            TextView textView = (TextView) convertView.findViewById(R.id.photosetTitle);
            
            Photoset set = getItem(position);
            
            new BitmapLoaderTask(set.getPrimaryPhoto().toURL(ImageURL.LARGE_SQUARE), imageView).execute();
            textView.setText(set.getTitle());
            return convertView;
        }
        
    }

}
